#include "file_path.h"

#include <memory.h>

// 40% of the time in JoinPath() is from calls with 2 arguments, so we
// specialize that case.
std::string JoinPath(std::string path1, std::string path2) {
    if (path1.empty()) return path2;
    if (path2.empty()) return path1;
    if (path1.back() == '/') {
        if (path2.front() == '/') return (path1 + path2.substr(1));
    } else {
        if (path2.front() != '/') return (path1 + "/" + path2);
    }
    return (path1 + path2);
}

namespace internal {

// Given a collection of file paths, append them all together,
// ensuring that the proper path separators are inserted between them.
std::string JoinPathImpl(bool honor_abs,
                         std::initializer_list<std::string> paths) {
    std::string result;

    if (paths.size() != 0) {
        // This size calculation is worst-case: it assumes one extra "/" for
        // every path other than the first.
        size_t total_size = paths.size() - 1;
        for (const std::string& path : paths) total_size += path.size();
        result.resize(total_size);

        auto begin = result.begin();
        auto out = begin;
        bool trailing_slash = false;
        for (std::string path : paths) {
            if (path.empty()) continue;
            if (path.front() == '/') {
                if (honor_abs) {
                    out = begin;  // wipe out whatever we've built up so far.
                } else if (trailing_slash) {
                    path.erase(0, 1);
                }
            } else {
                if (!trailing_slash && out != begin) *out++ = '/';
            }
            const size_t this_size = path.size();
            memcpy(&*out, path.data(), this_size);
            out += this_size;
            trailing_slash = out[-1] == '/';
        }
        result.erase(out - begin);
    }
    return result;
}

// Return the parts of the basename of path, split on the final ".".
// If there is no "." in the basename or "." is the final character in the
// basename, the second value will be empty.
std::pair<std::string, std::string> SplitBasename(std::string path) {
    path = Basename(path);

    std::string::size_type pos = path.find_last_of('.');
    if (pos == std::string::npos) {
        // std::string ext = path.substr(path.size(), 0);
        std::string ext = "";
        return std::make_pair(path, ext);
    }
    return std::make_pair(path.substr(0, pos), path.substr(pos + 1));
}

}  // namespace internal

std::string Dirname(std::string& path) { return SplitPath(path).first; }

std::string Basename(const std::string& path) { return SplitPath(path).second; }

std::pair<std::string, std::string> SplitPath(const std::string& path) {
    std::string::size_type pos = path.find_last_of('/');

    // Handle the case with no '/' in 'path'.
    if (pos == std::string::npos) {
        // std::string p = path.substr(0, 0);
        std::string p = "";
        return std::make_pair(p, path);
    }

    // Handle the case with a single leading '/' in 'path'.
    if (pos == 0) return std::make_pair(path.substr(0, 1), path.substr(1));

    return std::make_pair(path.substr(0, pos), path.substr(pos + 1));
}

std::string Extension(std::string& path) {
    return internal::SplitBasename(path).second;
}